<%--
  Created by IntelliJ IDEA.
  User: Dima
  Date: 15.01.2015
  Time: 3:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

<tiles:insertDefinition name="mainTemplate">
  <tiles:putAttribute name="body">

<div class="content">
    <div id="articles">
        <ul id="top-list">
            <li>
                <a href="#singleton">Singleton pattern </a>
            </li>
            <li>
                <a href="#strategy">Strategy pattern</a>
            </li>
            <li>
                <a href="#template_method">Templatee method</a>
            </li>
        </ul>


        <p>
            <a name="singleton" href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank" class="external-link tooltip articleTitle"> Singleton pattern <span>Link to wikipedia article</span></a>
        </p>

        <header>
            <h3 style="text-align:center">Preface </h3>
        </header>

        <p>
        <div>
    <span style="padding: 0px 5px 5px 0px; float:left;">
      <img src="/images/singleton.png"/>
    </span>
            <div>
                <p>
                    In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.
                    Implementation of a singleton pattern must satisfy the single instance and global access principles. It requires a mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among class objects. The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made private. Note the distinction between a simple static instance of a class and a singleton: although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed.
                </p>
                <p>
                    The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one. If the programming language has concurrent processing capabilities the method should be constructed to execute as a mutually exclusive operation. The classic solution to this problem is to use mutual exclusion on the class that indicates that the object is being instantiated.
                </p>
            </div>
        </div>
        </p>




        <table style="margin:0 auto;">
            <caption>Singleton implementations</caption>
            <colgroup>
                <col id="implementationCol" />
                <col id="lazyCol" />
                <col id="safetyCol" />
            </colgroup>
            <thead>
            <tr>
                <th scope="col">Implementation</th>
                <th scope="col">Lazy init</th>
                <th scope="col">Thread-safety</th>
            </tr>


            </thead>
            <tbody>
            <tr class="odd">
                <td>On Demand Holder idiom</td>
                <td class="yes">yes</td>
                <td class="yes">yes</td>
            </tr>
            <tr class="even">
                <td>Synchronized Accessor</td>
                <td class="yes">yes</td>
                <td class="yes">yes</td>
            </tr>
            <tr class="odd">
                <td>Enum Singleton</td>
                <td class="no">no</td>
                <td class="yes">yes</td>
            </tr>

            </tbody>
        </table>

        <p>On Demand Holder idiom</p>
<pre><span>On Demand Holder idiom</span>public class Singleton {

    public static class SingletonHolder {
        public static final Singleton HOLDER_INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.HOLDER_INSTANCE;
    }
}
</pre>

<pre><span>Synchronized Accessor</span>public class Singleton {
    private static Singleton instance;

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
</pre>


<pre><span>Enum Singleton</span>public enum Singleton {
    INSTANCE;
}
</pre>


        <p>
            <a name="strategy" href="http://en.wikipedia.org/wiki/Strategy_pattern" target="_blank" class="external-link tooltip articleTitle"> Strategy pattern <span>Link to wikipedia article</span></a>
        </p>


        <p>
            <a name="template_method" href="http://en.wikipedia.org/wiki/Template_method_pattern" target="_blank" class="external-link tooltip articleTitle"> Template method pattern pattern <span>Link to wikipedia article</span></a>
        </p>


    </div>
</div>

  </tiles:putAttribute>

  <tiles:putListAttribute name="scripts" inherit="true">
    <tiles:addAttribute value="/js/articles.js"/>
  </tiles:putListAttribute>


</tiles:insertDefinition>
